package com.ruoyi.web.controller.openai;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.feign.KnowledgeSessionServiceClient;
import com.ruoyi.system.feign.model.FeignKnowledgeSessionModel;
import com.ruoyi.system.feign.result.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/openai/knowledgeSession")
@RequiredArgsConstructor
public class KnowledgeSessionController extends BaseController {
    private final KnowledgeSessionServiceClient knowledgeSessionServiceClient;
    private final static String prefix = "openai/knowledgeSession";


    //    @RequiresPermissions("openai:knowledgeSession:view")
    @GetMapping()
    public String knowledgeSession() {
        return prefix + "/knowledgeSession";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FeignKnowledgeSessionModel query) {
        PageHelper<FeignKnowledgeSessionModel> pageHelper = new PageHelper<>();
        pageHelper.setPager(new Pager().setPage_num(1).setPage_size(10));
        pageHelper.setSorter(new Sorter().setOrders(List.of(new Order().setProperty("created_at").setDirection("desc"))));
        pageHelper.setQuery(query);
        FeignDataResult<FeignKnowledgeSessionModel> result = knowledgeSessionServiceClient.find(pageHelper);
        return getDataTable(result);
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    @Log(title = "知识库会话管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FeignKnowledgeSessionModel model) {
        knowledgeSessionServiceClient.save(model);
        return toAjax(1);
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        FeignObjectResult<FeignKnowledgeSessionModel> result = knowledgeSessionServiceClient.detail(id);
        mmap.put("knowledgeSession", result);
        return prefix + "/edit";
    }

    @Log(title = "知识库会话管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FeignKnowledgeSessionModel model) {
        knowledgeSessionServiceClient.update(model);
        return toAjax(1);
    }

    @Log(title = "知识库会话管理", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") String id) {
        knowledgeSessionServiceClient.delete(id);
        return toAjax(1);
    }

    //===================================private=============================================
    private TableDataInfo getDataTable(FeignDataResult<FeignKnowledgeSessionModel> result) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(result.getData().getContent() != null ? result.getData().getContent() : List.of());
        rspData.setTotal(result.getData().getPager().getTotal_record());
        return rspData;
    }

}
